//
//  DetialFoodViewController.m
//  南京大牌档
//
//  Created by student on 14-1-12.
//  Copyright (c) 2014年 无锡第三团队. All rights reserved.
//

#import "DetialFoodViewController.h"

@interface DetialFoodViewController ()

@end

@implementation DetialFoodViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor colorWithRed:255/255.0 green:215/255.0 blue:0/255.0 alpha:1];    self.view.frame=CGRectMake(0, 0, 250, 300);
    collectionFoodArray=[[NSMutableArray alloc]init];
    praiseFoodArray=[[NSMutableArray alloc]init];
    [self initDetialView];
}
-(void)initDetialView
{
    UIImageView *detialFoodImage=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 240, 180)];
    [detialFoodImage setImageWithURL:[NSURL URLWithString:_detialFoodArray[5]]];
    [self.view addSubview:detialFoodImage];
    
    UIButton *cancelButton=[[UIButton alloc]initWithFrame:CGRectMake(240, -10, 20, 20)];
    [cancelButton setImage:[UIImage imageNamed:@"xx"] forState:UIControlStateNormal];
    [self.view addSubview:cancelButton];
    
    UILabel *noticeeLabel=[[UILabel alloc]initWithFrame:CGRectMake(5, 190, 240, 10)];
    noticeeLabel.text=@"图片来源互联网,仅供参考!";
    noticeeLabel.backgroundColor=[UIColor clearColor];
    noticeeLabel.textAlignment=0;
    noticeeLabel.font=[UIFont boldSystemFontOfSize:7];
    [self.view addSubview:noticeeLabel];
    
    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(5, 205, 240, 20)];
    titleLabel.text=_detialFoodArray[1];
    titleLabel.backgroundColor=[UIColor clearColor];
    titleLabel.textAlignment=0;
    titleLabel.font=[UIFont boldSystemFontOfSize:15];
    [self.view addSubview:titleLabel];
    
    UIButton *collectionButton=[[UIButton alloc]initWithFrame:CGRectMake(10, 250, 100, 20)];
    collectionButton.layer.cornerRadius=5;
    collectionButton.tag=2;
    
    if ([[NSFileManager defaultManager]fileExistsAtPath:[self filePath:@"collectionFood.plist"]]) {
        
        collectionFoodArray=[NSMutableArray arrayWithContentsOfFile:[self filePath:@"collectionFood.plist"]];
        if ([collectionFoodArray containsObject:_detialFoodArray]) {
            [collectionButton setTitle:@"❤ 取消收藏" forState:UIControlStateNormal];
        }
        else{
            [collectionButton setTitle:@"❤ 收藏" forState:UIControlStateNormal];
        }
    }
    else{
        [collectionFoodArray writeToFile:[self filePath:@"collectionFood.plist"] atomically:YES];
        [collectionButton setTitle:@"❤ 收藏" forState:UIControlStateNormal];
    }
    
    collectionButton.titleLabel.font=[UIFont systemFontOfSize:10];
    [collectionButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    collectionButton.backgroundColor=[UIColor whiteColor];
    [collectionButton addTarget:self action:@selector(detialButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:collectionButton];
    
    praiseButton=[[UIButton alloc]initWithFrame:CGRectMake(140, 250, 100, 20)];
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    
      NSArray *array=[NSArray arrayWithObjects:@"142",@"166",@"169",@"215",@"167",@"54",@"55",@"57",@"168",@"181",@"89",@"17",@"145",@"125",@"56",@"127",@"180",nil];
    
    
    long int a;//排序影响系数
    long int b;//分类影响系数
    long int c=0;//口味影响系数
    long int d;//避免规律
    if ([_detialFoodArray[0] integerValue]<54) {
        a=400-[_detialFoodArray[0] integerValue];
        b=100;
            }
    else if ([_detialFoodArray[0] integerValue]<119){
        a=453-[_detialFoodArray[0] integerValue];
        b=99;
    }
    else if([_detialFoodArray[0] integerValue]<166){
        a=518-[_detialFoodArray[0] integerValue];
        b=98;
    }
    else if([_detialFoodArray[0] integerValue]<218){
        a=565-[_detialFoodArray[0] integerValue];
        b=97;
           }
    else if ([_detialFoodArray[0] integerValue]<301){
        a=617-[_detialFoodArray[0] integerValue];
        b=50;
    }
    else if ([_detialFoodArray[0] integerValue]<328){
        a=700-[_detialFoodArray[0] integerValue];
        b=49;
    }
    else{
        a=727-[_detialFoodArray[0] integerValue];
        b=48;
    }
    for (int i=0; i<array.count; i++) {
        if ([_detialFoodArray[0] isEqualToString:array[i]]) {
            c=500-a-i;
        }
    }
    long int hours=([dateComponent year]*1000000+[dateComponent month]*10000+[dateComponent day]*100+[dateComponent hour]-2014011314);
    for (int i=0; i<hours-1; i++) {
        d=arc4random()%3; //避免规律性
        praiseNum=praiseNum + ((a+b+c+d/10-423)/(177/239.0)+1)/24;
    }

    if ([[NSFileManager defaultManager]fileExistsAtPath:[self filePath:@"praiseFood.plist"]]) {
        praiseFoodArray=[NSMutableArray arrayWithContentsOfFile:[self filePath:@"praiseFood.plist"]];
        if ([praiseFoodArray containsObject:_detialFoodArray]){
        [praiseButton setTitle:[NSString stringWithFormat:@"👍 已赞(%d)",(int)praiseNum+1] forState:UIControlStateNormal];
            praiseButton.userInteractionEnabled=NO;
        }
    else{
        [praiseButton setTitle:[NSString stringWithFormat:@"👍 赞(%d)",(int)praiseNum] forState:UIControlStateNormal];        }
    }
    else{
        [praiseFoodArray writeToFile:[self filePath:@"praiseFood.plist"] atomically:YES];
        [praiseButton setTitle:[NSString stringWithFormat:@"👍 赞(%d)",(int)praiseNum] forState:UIControlStateNormal];
    }
    
    praiseButton.layer.cornerRadius=5;
    praiseButton.tag=3;
    praiseButton.titleLabel.font=[UIFont systemFontOfSize:10];
        [praiseButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    praiseButton.backgroundColor=[UIColor whiteColor];

    [self.view addSubview:praiseButton];

    
    [praiseButton addTarget:self action:@selector(detialButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)detialButtonPressed:(UIButton *)button
{
    if (button.tag==2) {
        collectionFoodArray=[NSMutableArray arrayWithContentsOfFile:[self filePath:@"collectionFood.plist"]];
        if ([collectionFoodArray containsObject:_detialFoodArray]) {
            [collectionFoodArray removeObject:_detialFoodArray];
            animationLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 250, 10, 10)];
            animationLabel.backgroundColor=[UIColor clearColor];
            animationLabel.text=@"-1";
            [self startAnimation];
            [button setTitle:@"❤ 收藏" forState:UIControlStateNormal];
            [collectionFoodArray writeToFile:[self filePath:@"collectionFood.plist"] atomically:YES];
        }
        else{
            animationLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 250, 10, 10)];
            animationLabel.text=@"+1";
            animationLabel.backgroundColor=[UIColor clearColor];
            [self startAnimation];
            [button setTitle:@"❤ 取消收藏" forState:UIControlStateNormal];
            [collectionFoodArray addObject:_detialFoodArray];
            [collectionFoodArray writeToFile:[self filePath:@"collectionFood.plist"] atomically:YES];
        }
    }
    else if(button.tag==3){
        animationLabel=[[UILabel alloc]initWithFrame:CGRectMake(230, 250, 10, 10)];
        animationLabel.text=@"+1";
        animationLabel.backgroundColor=[UIColor clearColor];
        animationLabel.font=[UIFont systemFontOfSize:8];
        animationLabel.textColor=[UIColor blackColor];
        [self.view addSubview:animationLabel];
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];
        animationLabel.frame=CGRectMake(230, 245, 10, 10);
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(removeLabel)];
        [UIView commitAnimations];
        [button setTitle:[NSString stringWithFormat:@"👍 已赞(%d)",(int)praiseNum+1] forState:UIControlStateNormal];
        [praiseFoodArray addObject:_detialFoodArray];
        [praiseFoodArray writeToFile:[self filePath:@"praiseFood.plist"] atomically:YES];
        praiseButton.userInteractionEnabled=NO;
    }
}
-(void)startAnimation
{
    animationLabel.font=[UIFont systemFontOfSize:8];
    animationLabel.textColor=[UIColor blackColor];
    [self.view addSubview:animationLabel];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    animationLabel.frame=CGRectMake(100, 245, 10, 10);
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(removeLabel)];
    [UIView commitAnimations];
}
-(void)removeLabel
{
    [animationLabel removeFromSuperview];
    
}

- (NSString*)filePath:(NSString*)fileName {
    NSArray* myPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* myDocPath = [myPaths objectAtIndex:0];
    NSString* filePath = [myDocPath stringByAppendingPathComponent:fileName];
    return filePath;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
